﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using TestMatrix.DataModel.Factory;
using TestMatrix.Model;
using TestMatrix.DataModel;
using SQLite;

namespace TestMatrix.BLController
{
    public static class ChartController
    {
        #region tblETLHistory
        public static List<tblETLHistory> QueryTblETLHistory(string sql)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            List<tblETLHistory> listDatas = ds.QueryTblETLHistory(sql);
            return listDatas;
        }
        #endregion

        #region tblBugList
        public static List<tblBugList> Query(string sql)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            List<tblBugList> listDatas = ds.QueryTblBugList(sql);
            return listDatas;
        }

        public static List<tblBugList> Query(string sql, params string[] args)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            List<tblBugList> listDatas = ds.QueryTblBugList(sql, args);
            return listDatas;
        }

        public static void InsertBugs(tblBugList bugs)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.InsertBugs(bugs);
        }

        public static void InsertAllBugs(List<tblBugList> bugs)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.InsertAllBugs(bugs);
        }

        public static void DeleteAllBugs()
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.DeleteAllBugs();
        }

        #endregion

        #region tblNumOfCreatedTC
        public static List<tblNumOfCreatedTC> QueryTblNumOfCreatedTC(string sql)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            List<tblNumOfCreatedTC> listDatas = ds.QueryTblNumOfCreatedTC(sql);
            return listDatas;
        }

        public static List<tblNumOfCreatedTC> QueryTblNumOfCreatedTC(string sql, params string[] args)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            List<tblNumOfCreatedTC> listDatas = ds.QueryTblNumOfCreatedTC(sql, args);
            return listDatas;
        }

        public static void InsertTblNumOfCreatedTC(tblNumOfCreatedTC tc)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.InsertTblNumOfCreatedTC(tc);
        }

        public static void InsertAllTblNumOfCreatedTC(List<tblNumOfCreatedTC> tc)
        {
            SQLiteConnection conn = DBHelper.getConnection();
            conn.InsertAll(tc);
        }

        public static void DeleteAllTblNumOfCreatedTC()
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.DeleteAllTblNumOfCreatedTC();
        }
        #endregion

        #region tblTestAutomation
        public static List<tblTestAutomation> QueryTblTestAutomation(string sql)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            List<tblTestAutomation> listDatas = ds.QueryTblTestAutomation(sql);
            return listDatas;
        }

        public static List<tblTestAutomation> QueryTblTestAutomation(string sql, params string[] args)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            List<tblTestAutomation> listDatas = ds.QueryTblTestAutomation(sql, args);
            return listDatas;
        }

        public static void InsertTblTestAutomation(tblTestAutomation tc)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.InsertTblTestAutomation(tc);
        }

        public static void InsertAllTblTestAutomation(List<tblTestAutomation> tc)
        {
            SQLiteConnection conn = DBHelper.getConnection();
            conn.InsertAll(tc);
        }

        public static void DeleteAllTblTestAutomation()
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.DeleteAllTblTestAutomation();
        }
        #endregion

        #region tblNumOfExecutedTC
        public static List<tblNumOfExecutedTC> QuerytblNumOfExecutedTC(string sql)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            List<tblNumOfExecutedTC> listDatas = ds.QuerytblNumOfExecutedTC(sql);
            return listDatas;
        }

        public static List<tblNumOfExecutedTC> QuerytblNumOfExecutedTC(string sql, params string[] args)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            List<tblNumOfExecutedTC> listDatas = ds.QuerytblNumOfExecutedTC(sql, args);
            return listDatas;
        }

        public static void InserttblNumOfExecutedTC(tblNumOfExecutedTC tc)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.InserttblNumOfExecutedTC(tc);
        }

        public static void InsertAlltblNumOfExecutedTC(List<tblNumOfExecutedTC> tc)
        {
            SQLiteConnection conn = DBHelper.getConnection();
            conn.InsertAll(tc);
        }

        public static void DeleteAlltblNumOfExecutedTC()
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.DeleteAlltblNumOfExecutedTC();
        }
        #endregion

        #region tblReleaseInfo 
        public static List<tblReleaseInfo> QuerytblReleaseInfo(string sql)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            List<tblReleaseInfo> listDatas = ds.QuerytblReleaseInfo(sql);
            return listDatas;
        }

        public static List<tblReleaseInfo> QuerytblReleaseInfo(string sql, params string[] args)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            List<tblReleaseInfo> listDatas = ds.QuerytblReleaseInfo(sql, args);
            return listDatas;
        }

        public static void InserttblReleaseInfo(tblReleaseInfo tc)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.InserttblReleaseInfo(tc);
        }

        public static void InsertAlltblReleaseInfo(List<tblReleaseInfo> rows)
        {
            SQLiteConnection conn = DBHelper.getConnection();
            conn.InsertAll(rows);
        }

        public static void UpdatetblReleaseInfo(tblReleaseInfo tc)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.UpdatetblReleaseInfo(tc);
        }

        public static void UpdateAlltblReleaseInfo(List<tblReleaseInfo> rows)
        {
            SQLiteConnection conn = DBHelper.getConnection();
            conn.UpdateAll(rows);
        }

        public static void DeleteAlltblReleaseInfo()
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.DeleteAlltblReleaseInfo();
        }

        #endregion

        #region tblReleaseDetailInfo
        public static List<tblReleaseDetailInfo> QuerytblReleaseDetailInfo(string sql)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            List<tblReleaseDetailInfo> listDatas = ds.QuerytblReleaseDetailInfo(sql);
            return listDatas;
        }

        public static List<tblReleaseDetailInfo> QuerytblReleaseDetailInfo(string sql, params string[] args)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            List<tblReleaseDetailInfo> listDatas = ds.QuerytblReleaseDetailInfo(sql, args);
            return listDatas;
        }

        public static void InserttblReleaseDetailInfo(tblReleaseDetailInfo tc)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.InserttblReleaseDetailInfo(tc);
        }

        public static void InsertAlltblReleaseDetailInfo(List<tblReleaseDetailInfo> rows)
        {
            SQLiteConnection conn = DBHelper.getConnection();
            conn.InsertAll(rows);
        }

        public static void UpdatetblReleaseDetailInfo(tblReleaseDetailInfo tc)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.UpdatetblReleaseDetailInfo(tc);
        }

        public static void UpdateAlltblReleaseDetailInfo(List<tblReleaseDetailInfo> rows)
        {
            SQLiteConnection conn = DBHelper.getConnection();
            conn.UpdateAll(rows);
        }

        public static void DeleteAlltblReleaseDetailInfo()
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.DeleteAlltblReleaseDetailInfo();
        }

        #endregion

        #region tblTestCaseInCI
        public static List<tblTestCaseInCI> QuerytblTestCaseInCI(string sql)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            List<tblTestCaseInCI> listDatas = ds.QuerytblTestCaseInCI(sql);
            return listDatas;
        }

        public static List<tblTestCaseInCI> QuerytblTestCaseInCI(string sql, params string[] args)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            List<tblTestCaseInCI> listDatas = ds.QuerytblTestCaseInCI(sql, args);
            return listDatas;
        }

        public static void InserttblTestCaseInCI(tblTestCaseInCI tc)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.InserttblTestCaseInCI(tc);
        }

        public static void InsertAlltblTestCaseInCI(List<tblTestCaseInCI> rows)
        {
            SQLiteConnection conn = DBHelper.getConnection();
            conn.InsertAll(rows);
        }

        public static void UpdateAlltblTestCaseInCI(List<tblTestCaseInCI> rows)
        {
            SQLiteConnection conn = DBHelper.getConnection();
            conn.UpdateAll(rows);
        }

        public static void DeleteAlltblTestCaseInCI()
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.DeleteAlltblTestCaseInCI();
        }

        #endregion

        #region tblGolbalTestCasePassRate
        public static List<tblGlobalTestCasePassRate> QuerytblGolbalTestCasePassRate(string sql)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            List<tblGlobalTestCasePassRate> listDatas = ds.QuerytblGolbalTestCasePassRate(sql);
            return listDatas;
        }

        public static List<tblGlobalTestCasePassRate> QuerytblGolbalTestCasePassRate(string sql, params string[] args)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            List<tblGlobalTestCasePassRate> listDatas = ds.QuerytblGolbalTestCasePassRate(sql, args);
            return listDatas;
        }

        public static void InserttblGolbalTestCasePassRate(tblGlobalTestCasePassRate tc)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.InserttblGolbalTestCasePassRate(tc);
        }

        public static void InsertAlltblGolbalTestCasePassRate(List<tblGlobalTestCasePassRate> tc)
        {
            SQLiteConnection conn = DBHelper.getConnection();
            conn.InsertAll(tc);
        }

        public static void DeleteAlltblGolbalTestCasePassRate()
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.DeleteAlltblGolbalTestCasePassRate();
        }

        #endregion

        #region tblGlobalTestCaseAutomation
        public static List<tblGlobalTestCaseAutomation> QuerytblGlobalTestCaseAutomation(string sql)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            List<tblGlobalTestCaseAutomation> listDatas = ds.QuerytblGlobalTestCaseAutomation(sql);
            return listDatas;
        }

        public static List<tblGlobalTestCaseAutomation> QuerytblGlobalTestCaseAutomation(string sql, params string[] args)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            List<tblGlobalTestCaseAutomation> listDatas = ds.QuerytblGlobalTestCaseAutomation(sql, args);
            return listDatas;
        }

        public static void InserttblGlobalTestCaseAutomation(tblGlobalTestCaseAutomation tc)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.InserttblGlobalTestCaseAutomation(tc);
        }

        public static void InsertAlltblGlobalTestCaseAutomation(List<tblGlobalTestCaseAutomation> tc)
        {
            SQLiteConnection conn = DBHelper.getConnection();
            conn.InsertAll(tc);
        }

        public static void DeleteAlltblGlobalTestCaseAutomation()
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.DeleteAlltblGlobalTestCaseAutomation();
        }

        #endregion

        #region tblGlobalDRE
        public static List<tblGlobalDRE> QuerytblGlobalDRE(string sql)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            List<tblGlobalDRE> listDatas = ds.QuerytblGlobalDRE(sql);
            return listDatas;
        }

        public static List<tblGlobalDRE> QuerytblGlobalDRE(string sql, params string[] args)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            List<tblGlobalDRE> listDatas = ds.QuerytblGlobalDRE(sql, args);
            return listDatas;
        }

        public static void InserttblGlobalDRE(tblGlobalDRE tc)
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.InserttblGlobalDRE(tc);
        }

        public static void InsertAlltblGlobalDRE(List<tblGlobalDRE> tc)
        {
            SQLiteConnection conn = DBHelper.getConnection();
            conn.InsertAll(tc);
        }

        public static void DeleteAlltblGlobalDRE()
        {
            DataSources ds = new DataSources(DataSourceType.SQLite);
            ds.DeleteAlltblGlobalDRE();
        }

        #endregion

        public static void WriteSettingToXml(List<PhoneSetting> data)
        {
            // Write to the Isolated Storage
            XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
            xmlWriterSettings.Indent = true;

            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile(ConstantDefinition.DATE_SETTING_FILEN_AME, FileMode.Create))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<PhoneSetting>));
                    using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
                    {
                        serializer.Serialize(xmlWriter, data);
                    }
                }
            }
        }

        public static string ReadSettingFromXml()
        {
            string strOutPut = "";
            try
            {
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile(ConstantDefinition.DATE_SETTING_FILEN_AME, FileMode.Open))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(List<PhoneSetting>));
                        List<PhoneSetting> data = (List<PhoneSetting>)serializer.Deserialize(stream);
                        
                        foreach (var t in data)
                        {
                            strOutPut = t.DateFilterType.ToString();
                        }
                    }
                }
            }
            catch
            {
                strOutPut = ConstantDefinition.DATE_FILTER_TYPE;
                return strOutPut;
            }
            return strOutPut;
        }
    }
}
